#ifndef AT_NETWORK_MQTT_ROUTE_h
#define AT_NETWORK_MQTT_ROUTE_h

#include "AtNetworkUrc.h"

#include "AtNetworkMqttTopic.h"
#include "AtNetworkMqttClient.h"

/**
 * MQTT URC内容长度定义
 */
#ifndef MQTT_TOPIC_URC_SIZE
#define MQTT_TOPIC_URC_SIZE 16
#endif

/**
 * MQTT 最大主题定义
 */
#ifndef MQTT_TOPIC_NUM_SIZE
#define MQTT_TOPIC_NUM_SIZE 32
#endif

#define TOPIC_LEN sizeof(AtNetworkMqttTopic *)
#define TOPIC_ITEM AtNetworkMqttTopic **
#define TOPIC_SIZE size_t

class AtNetworkMqttRoute : public AtNetworkUrc {
  protected:
    AtNetworkMqttClient *client;

    TOPIC_ITEM topicArr; // 订阅主题回调列表
    TOPIC_SIZE topicNum; // 订阅主题回调数量

  public:
    /**
     * 取消订阅主题
     */
    inline bool unsubscribe(AtNetworkMqttTopic *topic) {
      for (size_t i = 0; i < this->topicNum; i++) {
        if (this->topicArr[i] == topic) {
          this->topicArr[i] = NULL;

          return true;
        }
      }

      return false;
    }

    /**
     * 订阅主题
     */
    inline bool subscribe(AtNetworkMqttTopic *topic) {
      for (uint8_t i = 0; i < this->topicNum; i++) {
        if (this->topicArr[i] == topic) {
          return true;
        }
      }
      
      for (uint8_t i = 0; i < this->topicNum; i++) {
        if (this->topicArr[i] == NULL) {
          this->topicArr[i] = topic;

          return true;
        }
      }

      return false;
    }

    /**
     * URC回调接口函数
     */
    virtual void callback(void *param) = 0;
};

#endif